package consys.social.core;

import consys.social.core.bo.Entity;
import consys.social.core.dao.FollowDaoImpl;
import consys.social.core.service.AuthenticatedUser;
import consys.social.core.service.FollowServiceImpl;
import consys.social.memcached.dao.spymemcached.SpyMemcachedImpl;
import consys.social.nosql.dao.hbase.HBase;
import consys.social.nosql.dao.hbase.HBaseTable;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class App extends HttpServlet{
    private static final long serialVersionUID = -3049636220738739954L;

    @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException{

        String [] servers = {"localhost:11211"};
        SpyMemcachedImpl mem = new SpyMemcachedImpl("binary", servers);
        mem.setCacheOperationTimeoutSeconds(2);
        mem.setDefaultExpirationTimeSeconds(3600);

        FollowServiceImpl fs = new FollowServiceImpl();
        FollowDaoImpl fd = new FollowDaoImpl();
        ThreadLocal<HBase> ent = new ThreadLocal<HBase>();
        HBaseTable ht = new HBaseTable("entities");
        ent.set(ht);
        fd.setEntities(ent);
        fd.setMemcached(mem);
        fs.setFollowDao(fd);

        PrintWriter out = response.getWriter();

        Entity a = new Entity("a");

        for (int i = 0; i < 100; i++) {
            try {
                Entity follower = new Entity(new Integer(i).toString());
                AuthenticatedUser.setUser(follower);
                fs.follow(follower, a);
            } catch (NotAuthorizedException ex) {
            }
        }

        fs.getRandomFollowers(a, 15);
        fs.getAllFollowersUid(a);
        fs.getFollowers(a, 0, 25);


        out.println("<html>");
        out.println("<body>");
        out.println("<h1>Spring Dependency Injection into Servlet Listenner</h1>");
        out.println("</body>");
        out.println("</html>");

   }
}
